﻿using System;
using System.Collections.Generic;
using System.Text;
using SbsSW.SwiPlCs;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;

namespace Prolog
{
    class ConnectProlog
    {
        public ConnectProlog()
        {

        }
        public string Result(string s)
        {
            s.Trim();
            Regex r = new Regex(@"[A-Z_][a-zA-Z_]*");
            MatchCollection matches = r.Matches(s);
            string result="";
            try
            {
                PlQuery q = new PlQuery(s);
                bool HasSolution = false;
                foreach(PlQueryVariables v in q.SolutionVariables)
                {
                    HasSolution = true;
                    foreach(Match match in matches)
                    {
                        result+=match.ToString()+" = " +v[match.ToString()].ToString()+";";
                    }
                    result += "\r\n";
                }
                if (matches.Count == 0)
                    return HasSolution?"true":"false";
                return result;

            }
            catch (SbsSW.SwiPlCs.Exceptions.PlException ex)
            {
                return "Error query: " + ex.Message;
            }
        }
        public void InputFile(string s)
        {
            string query = s.Replace("\\", "//"); 
    
            try
            {
                PlQuery.PlCall(query);
            }
            catch(SbsSW.SwiPlCs.Exceptions.PlException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
